﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TCPVK02 : ViewBase
    {
        public string FROMDATE = "", ENDDATE = "", PART = "", ARBE = "";
        #region ==WSART===========
        public static readonly DependencyProperty WSARTProperty = DependencyProperty.Register("WSART",
            typeof(string), typeof(TCPVK02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWSARTChanged));
        public string WSART
        {
            get
            {
                return (string)GetValue(WSARTProperty);
            }
            set
            {
                SetValue(WSARTProperty, value);
                LOADVKP();
            }
        }
        private static void OnWSARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TCPVK02;
            if (src != null)
            {
                src.WSART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwWBH===========
        public static readonly DependencyProperty dwGABProperty = DependencyProperty.Register("dwGAB",
            typeof(DataRow), typeof(TCPVK02),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwGABChanged));
        public DataRow dwGAB
        {
            get
            {
                return (DataRow)GetValue(dwGABProperty);
            }
            set
            {
                SetValue(dwGABProperty, value);
            }
        }
        private static void OndwGABChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TCPVK02;
            if (src != null)
            {
                src.dwGAB = (DataRow)e.NewValue;
            }
        }
        #endregion

        ITXEXRPT ITXEXRPT;
        public TCPVK02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVKPOS();
            this.DataContext = MV; 
            InitializeComponent();

        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
        }

        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            QueryCommand(null, null);

        }
        public void LOADVKP()
        {
            if (WSART.Length<32)
            {
                TBMain.Clear();
                return;
            }
            dwData = TBMain.NewRow();
            dwData["VKPART"] = WSART.Substring(0,20);
            dwData["VKPFARBE"] = WSART.Substring(22, 10);
            try
            {
                SResult = ((VVKPOS)MV).QueryVKPOS(dwData, 16);
                MergeData(SResult, true, true);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        } 
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                FROMDATE = "";
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void PrintCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ITXEXRPT == null)
                {
                    ITXEXRPT = new ITXEXRPT();
                    ITXEXRPT.Root = "C:\\IntexReport";
                    ITXEXRPT.strCompany = app.company;
                    ITXEXRPT.PrintType = "OpenExcel";
                }
                ITXEXRPT.dsSource = new DataSet();
                ITXEXRPT.dsSource.Merge(MV.SetProd);
                //ITXEXRPT.dsSource.Merge(Se.Env);

                ITXEXRPT.DocType = "JLTCPWB02";

                ITXEXRPT.strExcelFile = @"JLT\JLTCPWB02.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ENDDATE = "";
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

    }
}
